package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import connectionMSqL.MySQLConn;
import entity.Professor;
import entity.Usuario;

public class ProfessorDAO {

	public static boolean insert(Professor p) {
		boolean salvou = false;

		Connection conn = MySQLConn.getConn();
		PreparedStatement ps = null;
		try {
			int paramIndex = 1;
			ps = conn
					.prepareStatement("INSERT INTO Professor (cpf,nome,endereco,email,telefone)"
							+ "VALUES (?,?,?,?,?)");
			ps.setString(paramIndex++, p.getCpf());
			ps.setString(paramIndex++, p.getNome());
			ps.setString(paramIndex++, p.getEndereco());
			ps.setString(paramIndex++, p.getEmail());
			ps.setString(paramIndex++, p.getTelefone());

			ps.executeUpdate();
			salvou = true;

		} catch (SQLException e) {
			System.out.println(e.getMessage());
		} finally {
			try {
				if (ps != null)
					ps.close();
				if (conn != null)
					ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return salvou;
	}

	public static Professor createProfessor(ResultSet rs) throws SQLException {
		Professor prof = new Professor();
		prof.setId(rs.getInt("id"));
		prof.setCpf(rs.getString("cpf"));
		prof.setNome(rs.getString("nome"));
		prof.setEndereco(rs.getString("endereco"));
		prof.setEmail(rs.getString("email"));
		prof.setTelefone(rs.getString("telefone"));

		return prof;
	}

	public static boolean save(Professor p) {
	
		boolean salvou = false;
		Connection conn = MySQLConn.getConn();
		PreparedStatement ps = null;
		try {
			int paramIndex = 1;
			ps = conn
					.prepareStatement("INSERT INTO professor ('cpf','nome','endereco','email','telefone')"
							+ "VALUES (?,?,?,?,?)");
			ps.setString(paramIndex++, p.getCpf());
			ps.setString(paramIndex++, p.getNome());
			ps.setString(paramIndex++, p.getEndereco());
			ps.setString(paramIndex++, p.getEmail());
			ps.setString(paramIndex++, p.getTelefone());

			ps.executeUpdate();
			salvou = true;

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ps != null)
					ps.close();
				if (conn != null)
					ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return salvou;
	}

	public static List<Professor> getAll() {
		List<Professor> professores = new ArrayList<Professor>();
		Connection conn = MySQLConn.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("SELECT * FROM professor");
			rs = ps.executeQuery();
			while (rs.next()) {

				Professor p = createProfessor(rs);
				professores.add(p);
			}

		} catch (SQLException e) {
			System.out.println(e.getMessage());
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ps != null)
					ps.close();
				if (conn != null)
					conn.close();

			} catch (SQLException e) {
				System.out.println(e.getMessage());
			}
		}

		return professores;
	}

	public static boolean update(Professor p) {
		boolean salvou = false;
		Connection conn = MySQLConn.getConn();
		PreparedStatement ps = null;
		try {
			String query = ("UPDATE professor" + " SET cpf='" + p.getCpf() + "'"
					+ ", nome='" + p.getNome() + "'" + ", endereco ='"
					+ p.getEndereco() + "'" + ", email='" + p.getEmail() + "'"
					+ ", telefone='" + p.getTelefone()+"'" + " WHERE id =" + p.getId());
			ps = conn.prepareStatement(query);
			ps.executeUpdate();
			salvou = true;
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		} finally {
			try {
				if (ps != null)
					ps.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				System.out.println(e.getMessage());
			}
		}
		return salvou;
	}

	public static List<Professor> findById(int id) {
		
		List<Professor> professores = new ArrayList<Professor>();
		Connection conn = MySQLConn.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("SELECT * FROM professor WHERE id="+id);
			rs = ps.executeQuery();
			while (rs.next()) {

				Professor p = createProfessor(rs);
				professores.add(p);
			}

		} catch (SQLException e) {
			System.out.println(e.getMessage());
			} finally {
			try {
				if (rs != null)
					rs.close();
				if (ps != null)
					ps.close();
				if (conn != null)
					conn.close();

			} catch (SQLException e) {
				System.out.println(e.getMessage());
			}
		}

		return professores;
	}
	

	public static boolean delete(int id) {
	
		Connection conn = MySQLConn.getConn();
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement("DELETE FROM professor WHERE id="+id);
			ps.executeUpdate();

		} catch (SQLException e) {
			System.out.println(e.getMessage());
			return false;
			} finally {
			try {
				if (ps != null)
					ps.close();
				if (conn != null)
					conn.close();

			} catch (SQLException e) {
				System.out.println(e.getMessage());
				return false;
			}
		}

		return true;
	}

	public static Professor findByIdRetObj(Integer id) {
		Professor professor = null;
		Connection conn = MySQLConn.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			ps = conn
					.prepareStatement(" SELECT * FROM professor WHERE id= " + id);
			rs = ps.executeQuery();
			if (rs.next()) {
				professor = createProfessor(rs);
				
			}

		} catch (SQLException e) {
			System.out.print("Erro ProfessorDAO.findByIdRetObj: " + e.getMessage());
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ps != null)
					ps.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				System.out.print("Erro ProfessorDAO.findByIdRetObj: " + e.getMessage());
			}
		}
		return professor;
		}
	


}//end